package com.pig4cloud.pigx.auth.handler;

import com.pig4cloud.pigx.common.security.handler.AuthenticationLogoutHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/8/5 9:58
 **/
@Slf4j
@Component
@AllArgsConstructor
public class PigxAuthenticationLogoutEventHandler implements AuthenticationLogoutHandler {

    /**
     * 退出登录事件处理
     *
     * @param authentication 认证信息
     * @param request        请求信息
     * @param response       响应信息
     */
    @Async
    @Override
    public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {

        String username = authentication.getName();

        // 获取clientId 信息
        OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
        String clientId = auth2Authentication.getOAuth2Request().getClientId();

        String token = request.getHeader(HttpHeaders.AUTHORIZATION);

        log.info("用户：{} 退出成功, 客户端:{},token:{}  已注销", username, clientId, token);

    }
}
